<?php

define( "FCAL_MAIN", 0);
define( "FCAL_ADD_EVENT", 1);
define( "FCAL_ADD_CALENDARIO", 2);
define( "FCAL_LOAD_DIV_BY_TIME", 3);
define( "FCAL_LOAD_CALENDARIOS", 4);
define( "FCAL_AJAX", 10);

define( "FCAL_TEST", 100);

define( "CAJAX_PRINT_MCAL", 0);
define( "CAJAX_PRINT_WEEK_BY_DAY", 1);
define( "CAJAX_PRINT_WEEK_BY_TIME", 2);

require_once '../mvc/calendar_view.php';
require_once '../mvc/add_event_controller.php';
require_once '../mvc/add_calendario_controller.php';

class CalendarController {
	private $calendar_view;

	private $add_event_controller;
	private $add_calendario_controller;

	private $usuario;			// Modelo Usuario
	
	function __construct( &$usuario) {
		$this->usuario =& $usuario;					// Obtenemos el 'modelo' de usuario.

		$this->calendar_view = new CalendarView();			// Crear la vista

		$this->add_event_controller = new AddEventController( $usuario);// Crear el controlador para agregar evento.
		$this->add_calendario_controller = new AddCalendarioController( $usuario);
	}
	
	function display() {
		$fcal = isset( $_GET['fcal']) ? $_GET['fcal'] : FCAL_MAIN;
		
		switch( $fcal) {
			default:
			case FCAL_MAIN:
				date_default_timezone_set('America/Santiago');
				$time = time();
				$year = date('Y', $time);
				$month = date('n', $time);
				$day = date('j',$time);

				$sem = $this->get_intervalo_sem( $year, $month, $day);
				$ls_event = $this->usuario->get_eventos( $sem);
				$ls_tareas = $this->usuario->get_tareas();

				$this->calendar_view->display( $year, $month, $day, $this->usuario->get_nickname(), $this->usuario->get_calendarios(), $ls_event, $ls_tareas);
				break;
			case FCAL_ADD_EVENT:
				$this->add_event_controller->display();
				break;
			case FCAL_ADD_CALENDARIO:
				echo $this->add_calendario_controller->display();
				break;
			case FCAL_LOAD_DIV_BY_TIME:
				$year = $_POST['year'];
				$month = $_POST['month'];
				$day = $_POST['day'];

				$sem = $this->get_intervalo_sem( $year, $month, $day);
				$ls_event = $this->usuario->get_eventos( $sem);

				echo $this->calendar_view->print_week_by_time( $year, $month, $day, $ls_event);
				break;
			case FCAL_LOAD_CALENDARIOS:
				echo $this->calendar_view->print_ls_calendarios( $this->usuario->get_calendarios());
				break;
			case FCAL_AJAX:
				switch( $_POST['fcal_ajax']) {
					case CAJAX_PRINT_MCAL:
						echo $this->calendar_view->print_mini_calendar( $_POST['year'], $_POST['month']);
						break;
					case CAJAX_PRINT_WEEK_BY_DAY:
						echo $this->calendar_view->print_week_by_day( $_POST['year'], $_POST['month'], $_POST['day']);
						break;
					case CAJAX_PRINT_WEEK_BY_TIME:
						$year = $_POST['year'];
						$month = $_POST['month'];
						$day = $_POST['day'];

						$sem = $this->get_intervalo_sem( $year, $month, $day);
						$ls_event = $this->usuario->get_eventos( $sem);

						echo $this->calendar_view->print_week_by_time( $year, $month, $day, $ls_event);
						break;
					default:break;
				}
				break;
			case FCAL_TEST:
				print_r( $this->usuario->get_eventos( $this->get_intervalo_sem(2012,7,18)));
				break;
		}
	}

	function print_mini_calendar( $year, $month) {
		return $this->calendar_view->print_mini_calendar( $year, $month);
	}

	function print_week_by_day( $year, $month, $day) {
		return $this->calendar_view->print_week_by_day( $year, $month, $day);
	}

	function print_week_by_time( $year, $month, $day) {
		return $this->calendar_view->print_week_by_time( $year, $month, $day);
	}

	function print_tittle( $year, $month, $day = 0) {
		return $this->calendar_view->print_tittle( $year, $month, $day);
	}

	function get_intervalo_sem( $year, $month, $day) {
		$gmmk_day = gmmktime( 0, 0, 0, $month, $day, $year);
		$weekday = gmstrftime('%w',$gmmk_day);

		$weekday = ($weekday + 6) % 7;

		$pdia = $day - $weekday;
		$gmmk_first_day = gmmktime( 0, 0, 0, $month, $pdia, $year);
		$gmmk_last_day = $gmmk_first_day + 6 * 86400;
	
		$sem[0] = gmstrftime('%Y-%m-%d 00:00:00',$gmmk_first_day);
		$sem[1] = gmstrftime('%Y-%m-%d 23:59:59',$gmmk_last_day);

		return $sem;
	}
	
}

?>
